<?php
namespace Admin\Controller;
use Think\Controller;

class QuestionlistController extends CommonController {
    public function index(){
        $data = I('get.');
    	if(isset($data['bgtime'])){
    			
			if($data['issearch']=='1'){
				$_GET['p']=1;
				unset($_GET['issearch']);
    		}
    		/*if($data['issearch']=='1'){
    			$search = true;
    		}else{
    			$search = false;
    		}*/
            if($data['bgtime']==''){
                $data['btime']=strtotime('1971-01-01');
            }else{
                $data['btime']=strtotime($data['bgtime']);
            }
            if($data['endtime']==''){
                $data['etime']=time();
            }else{
                $data['etime']=strtotime($data['endtime']);
            }
    		$where['create_time'] = array('between', array($data['btime'], $data['etime']));
            $sql_all = "where t1.isdel='0' and t1.create_time between ".$data['btime'].' and '.$data['etime'];
    		
			if($data['title']!=''){
				$where['title'] = array('LIKE','%'.$data['title'].'%');
                $sql_all.=' and title like "%'.$data['title'].'%" ';
			}
            $this->assign('title',$data['title']);
			$this->assign('bgtime',$data['bgtime']);
			$this->assign('endtime',$data['endtime']);
            $sql = "select t1.*,t2.cname,t3.nickname from question t1 left join category t2 on t1.cid=t2.cid left join user t3 on t1.uid=t3.uid ".$sql_all;
            $ques_search = M('question')->query($sql);
			$count = count($ques_search);
            $this->page($count,$sql,$search);
    	}else{
            $sql = "select t1.*,t2.cname,t3.nickname from question t1 left join category t2 on t1.cid=t2.cid left join user t3 on t1.uid=t3.uid where t1.isdel='0'";
       		$ques_all=M('question')->query($sql);
       		$count = count($ques_all);
            $this->page($count,$sql,false);
    	}
    }

    public function page($count,$sql,$search=false){
            $Page = new \Think\Page($count,3);// 实例化分页类 
            // if($search){
            // 	$Page->firstRow='0';
            // }
            $sql_ques =$sql.' order by t1.create_time limit '.$Page->firstRow.','.$Page->listRows;
            $ques = M('auth_group_access')->query($sql_ques);
            /*var_dump($ques);*/
            /*$admin = M('auth_group_access t1')->join('left join auth_group t2 on t1.group_id=t2.id')->select(); */
            $show = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $this->assign('ques',$ques);
            $this->assign('count',$count);
            $this->assign('list',$list);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display();
     }

     public function del(){
     	$qid = I('post.qid');

     	$res = M('question')->where(['qid'=>$qid])->setField(['isdel'=>'1']);

     	if($res){
     		$this->ajaxReturn(["error"=>0,"info"=>'删除成功']);
     	}else{
     		$this->ajaxReturn(["error"=>1,"info"=>'删除失败']);
     	}
     }

    public function delAll(){
    	$this->deleteAll('qid','question',true);
    }
}

?>